package com.xmtlxx.web.controller.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.web.controller.tms.service.IWaybillDtlService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.tms.domain.WaybillDtl;
import com.xmtlxx.tms.domain.bo.WaybillDtlBo;
import com.xmtlxx.tms.domain.vo.WaybillDtlVo;
import com.xmtlxx.tms.mapper.WaybillDtlMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 运单明细Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WaybillDtlServiceImpl implements IWaybillDtlService {

    private final WaybillDtlMapper baseMapper;

    /**
     * 查询运单明细
     *
     * @param id 运单明细主键
     * @return 运单明细
     */
    @Override
    public WaybillDtlVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectWaybillDtlVoByID(id);
    }

    /**
     * 查询运单明细列表
     *
     * @param bo 运单明细
     * @return 运单明细
     */
    @Override
    public TableDataInfo<WaybillDtlVo> queryPageList(WaybillDtlBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<WaybillDtl> lqw = buildQueryWrapper(bo);
       // Page<WaybillDtlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<WaybillDtlVo> result = baseMapper.selectPageWaybillDtlList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询运单明细列表
     *
     * @param bo 运单明细
     * @return 运单明细
     */
    @Override
    public List<WaybillDtlVo> queryList(WaybillDtlBo bo) {
       // LambdaQueryWrapper<WaybillDtl> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectWaybillDtlList(bo);
    }

    private LambdaQueryWrapper<WaybillDtl> buildQueryWrapper(WaybillDtlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WaybillDtl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWaybillId()), WaybillDtl::getWaybillId, bo.getWaybillId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderDtlId()), WaybillDtl::getOrderDtlId, bo.getOrderDtlId());
        lqw.eq(StringUtils.isNotBlank(bo.getPno()), WaybillDtl::getPno, bo.getPno());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WaybillDtl::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WaybillDtl::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), WaybillDtl::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增运单明细
     *
     * @param bo 运单明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(WaybillDtlBo bo) {
        WaybillDtl add = BeanUtil.toBean(bo, WaybillDtl.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改运单明细
     *
     * @param bo 运单明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(WaybillDtlBo bo) {
        WaybillDtl update = BeanUtil.toBean(bo, WaybillDtl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(WaybillDtl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除运单明细
     *
     * @param ids 需要删除的运单明细主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<WaybillDtlVo> queryList(String pno) {
//        List<WaybillDtlVo> waybillDtlVos = baseMapper.selectVoList(new LambdaQueryWrapper<WaybillDtl>().like(WaybillDtl::getPno,pno).eq(WaybillDtl::getSignStatus,"0"));
        WaybillDtlBo waybillDtl = new WaybillDtlBo();
        waybillDtl.setPno(pno);
        waybillDtl.setSignStatus("0");
        List<WaybillDtlVo> waybillDtlVos = baseMapper.selectWaybillDtlList(waybillDtl);
        if(waybillDtlVos != null && waybillDtlVos.size() > 0){
            waybillDtlVos.stream().forEach(item->{
                item.setOss(new ArrayList<>());
            });
        }
        return waybillDtlVos;
    }

    @Override
    public List<WaybillDtlVo> queryWaybillVos(List<String> ids,String waybillId) {
        return baseMapper.queryWaybillVos(ids,waybillId);
    }
}
